<?php


namespace app\admin\controller;

use app\common\model\SystemGroup;
use app\common\model\System as M;
use think\facade\Request;
use think\facade\View;

class SystemField extends Base
{
    protected $validate = 'System';
    //系统设置字段列表
    public function index(){
        if(Request::isPost()) {
            //全局查询条件
            $where = [];
            $key  = input('post.key');
            if (!empty($key)) {
                $where[] = ['name|field', 'like', '%' . $key . '%'];
            }
            $gid=input('post.gid');
            if (!empty($gid)) {
                $where[] = ['group_id', '=', $gid];
            }
            //分页
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $page = ['list_rows'=> $pageSize, 'page' => $page];

            //排序
            $sort_order = input('sort_order') ? input('sort_order') : 'asc';
            $order = input('sort_order')?[input('sort_by')=>$sort_order]:['sort'=>$sort_order];

            //获取列表
            $list = M::getList($where, $page,$order)->toArray();
            return ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        return view();
    }
    //修改状态
    public function status(){
        $data = input('post.');
        $result = M::editSave($data);
        return $result;
    }
    public function cf(){
        $data = input('param.');
        $result = M::editSave($data);
        return $result;
    }
    // 系统设置字段修改
    public function edit(){
        $id = input('param.id');
        $info = M::edit($id);
        //分组获取
        $systemGroup = SystemGroup::select();
        //字段类型获取
        $fildType = M::getType();
        $view =[
            'systemGroup' => $systemGroup,
            'fildType'    => $fildType,
            'info'        => $info,
        ];
        return view('form',$view);
    }

    public function editSave(){
        $data = Request::except(['file'], 'post');
        $result = $this->validate($data,$this->validate);
        if (true !== $result) {
            // 验证失败 输出错误信息
            return ['code'=>1,'msg'=>$result];
        } else {
            //特殊处理配置信息
            if (isset($data['setup'])) {
                $data['setup'] = array2string($data['setup']);
            }
            $result =  M::editSave($data);
            return $result;
        }
    }

    public function add(){
        //分组获取
        $systemGroup = SystemGroup::select();
        //字段类型获取
        $fildType = M::getType();
        if (!count($systemGroup)) {
            $this->error('请先添加系统设置分组');
        }
        $view = [
            'systemGroup' => $systemGroup,
            'fildType'    => $fildType,
            'info'        =>''
        ];
        return view('form',$view);
    }
    public function addSave(){
        $data = Request::except(['file'], 'post');
        $result = $this->validate($data, $this->validate);
        if (true !== $result) {
            // 验证失败 输出错误信息
            return ['code'=>1,'msg'=>$result];
        } else {
            //特殊处理配置信息
            if (isset($data['setup'])) {
                $data['setup'] = array2string($data['setup']);
            }
            $result =  M::addSave($data);
            return $result;
        }
    }

    public function del(){
        $id = input('param.id');
        return M::del($id);
    }
    public function delAll(){
        $ids = input('post.ids');
        return M::selectDel($ids);
    }

    public function fieldType(){
        View::assign(input('param.'));
        //根据name取值
        if (input('param.name')) {
            $fieldInfo = M::where('field','=',input('param.name'))->find()->toArray();
            $fieldInfo['setup'] = string2array($fieldInfo['setup']);
        } else {
            $fieldInfo = null;
        }
        $view = [
            'fieldInfo'  => $fieldInfo,
        ];
        View::assign($view);
        return view('fieldType');
    }


}